<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Request;

class HotelRoomPrice extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    protected function filter(&$map)
    {
        if ($this->request->param("hotel_id")) {
            $map['hotel_id'] = ["like", "%" . $this->request->param("hotel_id") . "%"];
        }
        if ($this->request->param("hotel_time_price_id")) {
            $map['hotel_time_price_id'] = ["like", "%" . $this->request->param("hotel_time_price_id") . "%"];
        }
        if ($this->request->param("room_type_id")) {
            $map['room_type_id'] = ["like", "%" . $this->request->param("room_type_id") . "%"];
        }
        if ($this->request->param("room_price")) {
            $map['room_price'] = ["like", "%" . $this->request->param("room_price") . "%"];
        }
        if ($this->request->param("num")) {
            $map['num'] = ["like", "%" . $this->request->param("num") . "%"];
        }
    }

    /**
     * 首页
     */
    public function index(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            unset($postData['id']);
            // 查找酒店类型id
            $roomTypeInfo = Db::name('hotel_room_price')->where('hotel_time_price_id',$postData['hotel_time_price_id'])
                ->where('room_type_id',$postData['room_type_id'])->find(); // 存在该房间房型就不能添加
            if ($roomTypeInfo){
                return ajax_return_adv_error('已存在该房型价格信息');
            }
            if (!$postData['hotel_time_price_id']){
                return ajax_return_adv_error('缺少参数');
            }
            if (!$postData['room_type_id']){
                return ajax_return_adv_error('缺少房型');
            }
            if (!$postData['room_price']){
                return ajax_return_adv_error('缺少房价');
            }

            // 查找酒店id
            $postData['hotel_id'] = Db::name('hotel_time_price')->where('id',$postData['hotel_time_price_id'])->value('hotel_id');
            $postData['create_time'] = time();

            $addId = Db::name('hotel_room_price')->strict(false)->insertGetId($postData);
            if ($addId){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_adv_error('添加失败');
            }
        }else{
            $data = input();
            if (!$data['id']){
                return ajax_return_adv_error('缺少必要的参数');
            }
            // 查询条件
//        if ($data['hotel_id']){
//
//        }
            $id = $data['id'];
            // 查询该结算价格的房价
            $vo = Db::name('hotel_room_price')->where('hotel_time_price_id',$data['id'])->order('id','desc')->where('isdelete',0)->select();
            // 查找酒店名称
            if ($vo){
                foreach ($vo as $index => $item) {
                    $vo[$index]['room_type_name'] = Db::name('hotel_room_type')->where('id',$item['room_type_id'])->value('room_name');
                    $vo[$index]['hotel_name'] = Db::name('hotel')->where('id',$item['hotel_id'])->value('hotel_name');
                }
            }
            // 查询结算价格信息
            $hotel_time_price = Db::name('hotel_time_price')->where('id',$id)->find();
            // 查找该酒店的房型
            $hotel_room_type = Db::name('room_hotel_number')->where('hotel_id',$hotel_time_price['hotel_id'])
                ->where('isdelete', 0)
                ->field('number_of_rooms,create_time,isdelete', true)
                ->select();
            if ($hotel_room_type) {
                foreach ($hotel_room_type as $index => $item) {
                    // 获取房型的名称
                    $hotel_room_type[$index]['type_name'] = Db::name('hotel_room_type')->where('id',$item['room_type_id'])->value('room_name');
                }
            }
            $count = count($vo);
            $this->view->assign('list',$vo);
            $this->view->assign('count',$count);
            $this->view->assign('hotel_room_type',$hotel_room_type);
            $this->view->assign('hotel_time_price', $hotel_time_price);
            $this->view->assign('id',$id);
            return $this->view->fetch();
        }

    }

    /**
 * 添加
 */
    public function add(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            unset($postData['id']);
            // 查找酒店类型id
            $roomTypeInfo = Db::name('hotel_room_price')->where('hotel_time_price_id',$postData['hotel_time_price_id'])
                ->where('room_type_id',$postData['room_type_id'])->find(); // 存在该房间房型就不能添加
            if ($roomTypeInfo){
                return ajax_return_adv_error('已存在该房型价格信息');
            }
            if (!$postData['hotel_time_price_id']){
                return ajax_return_adv_error('缺少参数');
            }
            if (!$postData['room_type_id']){
                return ajax_return_adv_error('缺少房型');
            }
            if (!$postData['room_price']){
                return ajax_return_adv_error('缺少房价');
            }

            // 查找酒店id
            $postData['hotel_id'] = Db::name('hotel_time_price')->where('id',$postData['hotel_time_price_id'])->value('hotel_id');
            $postData['create_time'] = time();
            $addId = Db::name('hotel_room_price')->strict(false)->insertGetId($postData);
            if ($addId){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_adv_error('添加失败');
            }
        }else{
            $data = input();
            // 查询结算价格信息
            $hotel_time_price = Db::name('hotel_time_price')->where('id',$data['hotel_time_price_id'])->find();
            // 查询酒店名称
            $hotel_name = Db::name('hotel')->where('id',$hotel_time_price['hotel_id'])->value('hotel_name');
            // 查找该酒店的房型
            $hotel_room_type = Db::name('room_hotel_number')->where('hotel_id',$hotel_time_price['hotel_id'])
                ->where('isdelete', 0)
                ->field('number_of_rooms,create_time,isdelete', true)
                ->select();
            if ($hotel_room_type) {
                foreach ($hotel_room_type as $index => $item) {
                    // 获取房型的名称
                    $hotel_room_type[$index]['type_name'] = Db::name('hotel_room_type')->where('id',$item['room_type_id'])->value('room_name');
                }
            }
            $this->view->assign('hotel_room_type',$hotel_room_type);
            $this->view->assign('hotel_name', $hotel_name);
            $this->view->assign('hotel_time_price', $hotel_time_price);
            return $this->view->fetch('edit');
        }
    }


    /**
     * 编辑
     */
    public function edit(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            if (!$postData['hotel_time_price_id']){
                return ajax_return_adv_error('缺少参数');
            }
            if (!$postData['room_type_id']){
                return ajax_return_adv_error('缺少房型');
            }
            if (!$postData['room_price']){
                return ajax_return_adv_error('缺少房价');
            }
            // 查找酒店类型id
            $roomTypeInfo = Db::name('hotel_room_price')->where('id','<>',$postData['id'])
                                                            ->where('hotel_time_price_id',$postData['hotel_time_price_id'])
                                                            ->where('room_type_id',$postData['room_type_id'])->find(); // 存在该房间房型就不能添加
            if ($roomTypeInfo){
                return ajax_return_adv_error('不能更新房型,因为别的价格已存在该房型');
            }
            // 查找酒店id
            $postData['hotel_id'] = Db::name('hotel_time_price')->where('id',$postData['hotel_time_price_id'])->value('hotel_id');
            $up = Db::name('hotel_room_price')->strict(false)->update($postData);
            return ajax_return_adv('编辑成功');
        }else{
            $data = input();
            // 查找改房价信息
            $hotelRoomPrice = Db::name('hotel_room_price')->where('id',$data['id'])->find();
            // 查询结算价格信息
            $hotel_time_price = Db::name('hotel_time_price')->where('id',$hotelRoomPrice['hotel_time_price_id'])->find();
            // 查询酒店名称
            $hotel_name = Db::name('hotel')->where('id',$hotelRoomPrice['hotel_id'])->value('hotel_name');
            // 查找该酒店的房型
            $hotel_room_type = Db::name('room_hotel_number')->where('hotel_id',$hotelRoomPrice['hotel_id'])
                ->field('number_of_rooms,create_time,isdelete', true)
                ->select();
            if ($hotel_room_type) {
                foreach ($hotel_room_type as $index => $item) {
                    // 获取房型的名称
                    $hotel_room_type[$index]['type_name'] = Db::name('hotel_room_type')->where('id',$item['room_type_id'])->value('room_name');
                }
            }
            $this->view->assign('vo',$hotelRoomPrice);
            $this->view->assign('hotel_room_type',$hotel_room_type);
            $this->view->assign('hotel_name', $hotel_name);
            $this->view->assign('hotel_time_price', $hotel_time_price);
            return $this->view->fetch('edit');
        }
    }

    /**
     * 保存编辑
     */
    public function save(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            // 验证数据
            if (!$postData['id']){
                return ajax_return_adv_error('缺少id');
            }
            if (!$postData['price']){
                return ajax_return_adv_error('价格不能为空');
            }
            if($postData['include_breakfast'] == 0 || $postData['include_breakfast']== 1){
                // 查找房型价格
                $find = Db::name('hotel_room_price')->where('id',$postData['id'])->find();
                if (!$find){
                    return ajax_return_adv_error('不存在该房型价格信息');
                }elseif ($find['room_price'] == $postData['price'] && $find['include_breakfast'] == $postData['include_breakfast']){
                    return ajax_return_adv('更新成功');
                }
                $up = Db::name('hotel_room_price')->where('id',$postData['id'])->update(['room_price'=>$postData['price'],'include_breakfast'=>$postData['include_breakfast']]);
                if ($up){
                    return ajax_return_adv('更新成功');
                }else{
                    return ajax_return_adv_error('更新失败');
                }
            }else{
                return ajax_return_adv_error('是否包含早餐只能选择‘是’或者‘否’');
            }
        }else{
            return ajax_return_adv_error('非法请求');
        }
    }
}
